//
//  SolarSeekValueTransformers.m
//  SolarSeek
//
//  Created by Erick Fejta on 05/03/19.
//  Copyright 2005 Erick Fejta. All rights reserved.
//

#import "SolarSeekValueTransformers.h"
#import "EnumTypes.h"
#import "Shares.h"

@implementation NSString (FejtaSorters)
- (NSComparisonResult)numericCompare:(NSString *)string {
	return [self compare:string options:NSNumericSearch];
}
@end

@implementation SolarSeekValueTransformerCardinalNumbers
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; } //just lazy, could be yes.
- (id)transformedValue:(id)object {
	if([object respondsToSelector:@selector(intValue)]) {
		NSString *suffix = @"th";
		switch(abs([object intValue])%10) {
			case 1:
				suffix = @"st";
				break;
			case 2:
				suffix = @"nd";
				break;
			case 3:
				suffix = @"rd";
				break;
		}
		return [NSString stringWithFormat:@"%d%@",[object intValue],suffix];
	}
	else
		return @"Non-Integer";
}

@end

@implementation SolarSeekValueTransformerMetric
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)object {
	if([object respondsToSelector:@selector(floatValue)]) {
		float count = [object floatValue];
		NSString *pre = @"";
		NSString *post = [self post];
		float div = 1024;
		float big = count*1.1;
		if(big > div) {
			pre = @"K";
			div*=1024.0;
			if(big > div) {
				pre = @"M";
				div*=1024.0;
				if(big > div) {
					pre = @"G";
					div*=1024.0;
					if(big > div) {
						pre = @"T";
						div*=1024;
						if(big > div) {
							pre = @"P";
							div*=1024;
						}
					}
				}
			}
		}
		div/=1024.0;
			
		return [NSString stringWithFormat:@"%.1f %@%@",(count/div),pre,post];
	}
	else {
		return [self nonNumber];
	}
}

- (NSString *)nonNumber { return @"Unknown"; }

@end


@implementation SolarSeekValueTransformerSize
- (NSString *)post { return @"B"; }
@end

@implementation SolarSeekValueTransformerSpeed
- (id)transformedValue:(id)object {
	if([object respondsToSelector:@selector(floatValue)]) {
		float count = [object floatValue];
		if(count==0)
			return @"-";
		else 
			return [super transformedValue:object];
	}
	else
		return [super transformedValue:object];
}

- (NSString *)post { return @"B/s"; }
@end

@implementation SolarSeekValueTransformerTimeAlternate
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	if(value && [value respondsToSelector:@selector(intValue)]) {
		int seconds = [value intValue];
		
		NSString *string = [NSString stringWithFormat:@"%d",seconds%60];
		int min = seconds/60;
		if(min > 0) {
			string = [NSString stringWithFormat:@"%d:%@",min,string];
			int hr = min/60;
			if(hr>0) {
				string = [NSString stringWithFormat:@"%d:%@",hr,string];
				int days = hr/24;
				if(days>0) {
					string = [NSString stringWithFormat:@"%d:%@",days,string];
				}
			}
		}
		return string;			
	}
	else if(value==nil)
		return @"-";
	else
		return [NSString stringWithFormat:@"Unknown Value: %@",value];
}
@end

@implementation SolarSeekValueTransformerTime
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	if([value respondsToSelector:@selector(doubleValue)]) {
		double d = [value doubleValue];
		double t = d*1.7;
		float div = 1;
		NSString *pre = @"sec";
		if(t >= 1.0 || t <= -1.0) {
			float  next = 60;
			if(t > next) {
				pre = @"min";
				div=next;
				next*=60;
				if(t > next) {
					pre = @"hrs";
					div = next;
					next*=24;
					if(t > next) {
						pre = @"days";
						div = next;
					}
				}
					
			}
		}
		else {
			pre = @"ms";
			div = 0.001;
		}
		
		return [NSString stringWithFormat:@"%.1f %@",(d/div),pre];
	}
	else
		return @"Unknown Time";
}
@end

@implementation SolarSeekValueTransformerLastPathComponent
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	NSString *s = [value description];
	return [[[s lastPathComponent] componentsSeparatedByString:@"\\"] lastObject];
}
@end

@implementation SolarSeekValueTransformerRemoveLastPathComponent
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	NSString *s = [value description];
	NSArray *a = [s componentsSeparatedByString:@"\\"];
	NSString *last = [[a lastObject] stringByDeletingLastPathComponent];
	return [[[a subarrayWithRange:NSMakeRange(0,[a count]-1)] arrayByAddingObject:last] componentsJoinedByString:@"\\"];
}
@end
@implementation SolarSeekValueTransformerAvailability
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	Availability i = [value intValue];
	switch(i) {
	case AvailabilityAway:
		return @"Away";
	case AvailabilityOnline:
		return @"Online";
	case AvailabilityOffline:
		return @"Offline";
	default:
		return @"Unknown";
	}	
}
@end


@implementation SolarSeekValueTransformerColorAvailable
+ (Class)transformedValueClass { return [NSColor self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
	int i = [value intValue];
	NSColor *c = [NSColor textColor];
	return i?c:[c blendedColorWithFraction:0.7 ofColor:[NSColor whiteColor]];
}
@end


@implementation SolarSeekValueTransformerAttributize
+ (Class)transformedValueClass { return [NSAttributedString self]; }
+ (BOOL)allowsReverseTransformation { return YES; }
- (id)transformedValue:(id)value {
	return [[[NSAttributedString alloc] initWithString:[value description]] autorelease];
}
- (id)reverseTransformedValue:(id)value {
	return [value string];
}
@end

@implementation SolarSeekValueTransformerBitrate
+ (Class)transformedValueClass { return [NSAttributedString self]; }
+ (BOOL)allowsReverseTransformation { return YES; }
- (id)transformedValue:(id)value {
	id o = [[(Share *)value attributes] objectForKey:@"bitrate"];
	NSString *string = @"-";
	if(o) {
		string = [NSString stringWithFormat:@"%d kbps", [o unsignedIntValue]];
		o = [[(Share *)value attributes] objectForKey:@"VBR"];
		if(o && [o boolValue]) {
			string = [NSString stringWithFormat:@"VBR (%@)",string];
		}
	}
	return [[[NSAttributedString alloc] initWithString:string] autorelease];
}
@end


@implementation SolarSeekValueTransformers 
- (void)install {
	NSMutableArray *ma = [NSMutableArray arrayWithCapacity:20];
	
	[ma addObject:[[[SolarSeekValueTransformerLastPathComponent alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerRemoveLastPathComponent alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerSize alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerSpeed alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerTime alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerTimeAlternate alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerCardinalNumbers alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerColorAvailable alloc] init] autorelease]];	
	[ma addObject:[[[SolarSeekValueTransformerAvailability alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerAttributize alloc] init] autorelease]];
	[ma addObject:[[[SolarSeekValueTransformerBitrate alloc] init] autorelease]];
	
	NSEnumerator *e = [ma objectEnumerator];
	NSValueTransformer *vt;
	while(vt = [e nextObject])
		[NSValueTransformer setValueTransformer:vt forName:NSStringFromClass([vt class])];

}

@end